﻿using GoogleMapsHelpers.Builders;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Mvc;

namespace GoogleMapsHelpers
{

    public static class GoogleMapsHelpersExtensions
    {
        #region Google Maps Helpers

        /// <summary>
        /// Renders a place holder for Google Maps
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static IHtmlString GoogleMaps(this HtmlHelper helper, object htmlAttributes)
        {
            IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

            var builder = new TagBuilder(ScriptConstants.Div);
            builder.MergeAttributes<string, object>(attributes);
            builder.MergeAttribute(ScriptConstants.Id, ScriptConstants.DefaultMapId);

            return new HtmlString(builder.ToString(TagRenderMode.Normal));
        }

        #endregion

        #region Google Maps API helpers

        public static IHtmlString GoogleMapsAPI(this HtmlHelper helper, string address)
        {
            return helper.GoogleMapsAPI(null, false, null, null, address, null);
        }

        public static IHtmlString GoogleMapsAPI(this HtmlHelper helper, string[] addresses)
        {
            return helper.GoogleMapsAPI(null, false, null, null, null, addresses);
        }

        public static IHtmlString GoogleMapsAPI(this HtmlHelper helper, int? zoom, MapTypes? mapTypes, string address)
        {
            return helper.GoogleMapsAPI(null, false, zoom, mapTypes, address, null);
        }

        public static IHtmlString GoogleMapsAPI(this HtmlHelper helper, int? zoom, MapTypes? mapTypes, string[] addresses)
        {
            return helper.GoogleMapsAPI(null, false, zoom, mapTypes, null, addresses);
        }

        public static IHtmlString GoogleMapsAPI(this HtmlHelper helper, int? zoom, MapTypes? mapTypes, string address, string[] addresses)
        {
            return helper.GoogleMapsAPI(null, false, zoom, mapTypes, address, addresses);
        }

        public static IHtmlString GoogleMapsAPI(this HtmlHelper helper, bool? sensorOption, int? zoom, MapTypes? mapTypes, string address, string[] addresses)
        {
            return helper.GoogleMapsAPI(null, sensorOption, zoom, mapTypes, address, addresses);
        }

        /// <summary>
        /// Renders Google Maps API scripts and option scripts
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="apiKey"></param>
        /// <param name="sensorOption"></param>
        /// <param name="zoom"></param>
        /// <param name="mapTypes"></param>
        /// <param name="address"></param>
        /// <param name="addresses"></param>
        /// <returns></returns>
        public static IHtmlString GoogleMapsAPI(this HtmlHelper helper, string apiKey, bool? sensorOption, int? zoom, MapTypes? mapTypes, string address, string[] addresses)
        {
            var builder = new GoogleMapsScriptBuilder();
            if (!string.IsNullOrEmpty(apiKey)) builder.AddApiKey(apiKey);

            if (sensorOption == null || sensorOption.Value == false) 
                builder.AddSensorOption(false);
            else
                builder.AddSensorOption(true);

            builder.AddMapOtiopn(zoom, mapTypes);

            if (!string.IsNullOrEmpty(address) && addresses == null)
                builder.AddMarkers(address);
            else if (string.IsNullOrEmpty(address) && addresses != null)
                builder.AddMarkers(addresses);

            return builder.GetResult();
        }

        #endregion
    }
}
